package fem2.observer;

import java.io.IOException;

import fem2.Model;
import fem2.Observer;
import fem2.pre_and_post.GidPostForPoissonProblem;

public class GidObserverForPoissonProblem implements Observer {

	protected GidPostForPoissonProblem gidPost;
	protected Model m;
	protected TimeStepObserver timeStepObserver;

	/**
	 * 
	 * @param m
	 * @param s
	 * @param projectDir
	 * @param projectName
	 */
	public GidObserverForPoissonProblem(Model m, TimeStepObserver o, String projectDir,
			String projectName) {
		this.m = m;
		this.timeStepObserver = o;
		gidPost = new GidPostForPoissonProblem(projectDir, projectName);
	}

	/**
	 * 
	 * @param m
	 * @param s
	 * @param projectDir
	 * @param projectName
	 * @param fileName
	 */
	public GidObserverForPoissonProblem(Model m, TimeStepObserver o, String projectDir,
			String projectName, String fileName) {
		this.m = m;
		this.timeStepObserver = o;
		gidPost = new GidPostForPoissonProblem(projectDir, projectName, fileName);
	}

	/**
	 * 
	 * @return the model
	 */
	public Model getModel() {
		return m;
	}

	@Override
	public void fireStarted() {
	}

	@Override
	public void fireTimeStepStarted() {
	}

	@Override
	public void fireIterationStarted() {
	}

	@Override
	public void fireIterationFinished() {
	}

	@Override
	public void fireTimeStepFinished() {
		/*
		 * output to file as needed
		 */
		if (gidPost != null) {
			double timeStep = timeStepObserver.getCurrentTimeStep();

			try {
				gidPost.writeMesh(m, timeStep);
				gidPost.writeNodalTemperatureResults(m, timeStep);
				gidPost.writeGaussPointHeatFluxResults(m, timeStep);
			} catch (IOException e) {
				throw new Error("Write failed: " + e.getMessage());
			}
		}
	}

	@Override
	public void fireFinished() {
	}

}
